package com.heer.gymnasium.module.reservation.views;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.card.beans.CardInfoResponseBean;
import com.heer.gymnasium.module.card.beans.StadiumCardResponsebean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.util.StringUtils;
import com.heer.gymnasium.util.WxShareUtils;
import com.heer.imagepick.DensityUtils;
import com.zbar.lib.encoding.EncodingUtils;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 订场入场券
 * Created by zy on 2018/1/26.
 */

public class TicketDetailActivity extends BaseActivity {

    private final static int RES_ID_ROOT = R.layout.layout_ticket_detail;
    private final static int RES_ID_QR = R.id.layout_ticker_detail_zbar_iv;
    private final static int RES_ID_TICKET_TYPE = R.id.layout_ticker_detail_name_tv;
    private final static int RES_ID_TICKET_TIME_LABEL = R.id.layout_ticker_detail_time_label;
    private final static int RES_ID_TICKET_TIME = R.id.layout_ticker_detail_time_tv;
    private final static int RES_ID_COUNT_LL = R.id.layout_ticker_detail_count_ll;
    private final static int RES_ID_COUNT_TV = R.id.layout_ticker_detail_count_tv;
    private final static int RES_ID_TICKET_ADDRESS = R.id.layout_ticker_detail_address_tv;
    private final static int RES_ID_TICKET_SPACE = R.id.layout_ticker_detail_space_tv;
    private final static int RES_ID_TICKET_SPACE_LL = R.id.layout_ticker_detail_space_ll;
    private final static int RES_ID_TICKET_IPHONE = R.id.layout_ticker_detail_iphone_iv;
    private final static int RES_ID_TICKET_SHARE = R.id.layout_ticker_detail_share_tv;

    @BindView(RES_ID_QR)
    ImageView qrImageView;
    @BindView(RES_ID_TICKET_TYPE)
    TextView ticketTypeTextView;
    @BindView(RES_ID_TICKET_TIME)
    TextView timeTextView;
    @BindView(RES_ID_TICKET_TIME_LABEL)
    TextView timeLabelTextView;
    @BindView(RES_ID_COUNT_TV)
    TextView countTextView;
    @BindView(RES_ID_TICKET_SPACE)
    TextView spaceTextView;
    @BindView(RES_ID_TICKET_ADDRESS)
    TextView addressTextView;
    @BindView(RES_ID_TICKET_IPHONE)
    TextView iphoneTextView;
    @BindView(RES_ID_TICKET_SHARE)
    TextView shareTextView;
    @BindView(RES_ID_COUNT_LL)
    View countView;
    @BindView(RES_ID_TICKET_SPACE_LL)
    View spaceView;

    private TicketDetailActivity $this;

    private String cardType;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_ROOT);
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.startMainActivity(this);
        cardType = getIntent().getStringExtra(EXTRA_TYPE);
        if (cardType.equals(Type.STADIUM_TICKET_TYPE) || cardType.equals(Type.STADIUM_TYPE) || cardType.equals(Type.STADIUM_YUE) || cardType.equals(Type.STADIUM_FELLOW)) {
            shareTextView.setVisibility(View.VISIBLE);
            countView.setVisibility(View.GONE);
            titleUtils.setTitleText("入场券");
        } else {
            shareTextView.setVisibility(View.GONE);
            countView.setVisibility(View.VISIBLE);
            titleUtils.setTitleText("卡详情");
        }
    }

    @Override
    protected void loadData() {
        super.loadData();
        requestData();
    }

    @OnClick(RES_ID_TICKET_SHARE)
    public void onClickForShare() {
        String id = getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(id)) {
            return;
        }
        WxShareUtils wxShareUtils = new WxShareUtils($this);
        wxShareUtils.wxShare(id, Type.SHARE_COUPON);
    }

    public static void startActivity(Context context, String id, String type) {
        Intent mIntent = new Intent(context, TicketDetailActivity.class);
        mIntent.putExtra(EXTRA_ID, id);
        mIntent.putExtra(EXTRA_TYPE, type);
        context.startActivity(mIntent);
    }

    //获得入场券详情
    private void requestData() {
        String id = getIntent().getStringExtra(EXTRA_ID);

        if (TextUtils.isEmpty(id)) {
            return;
        }
        if (cardType.equals(Type.STADIUM_TICKET_TYPE)) {
            getShareCardInfo(id);
        } else if (cardType.equals(Type.STADIUM_YUE)) {
            getMatchCardInfo(id);
        } else {
            getMyCardInfo(id);
        }
    }

    //获得我的卡详情
    private void getMyCardInfo(String id) {
        mAppHelper.apiService().getMyCardInfo(id).enqueue(new BaseCallback<ApiResponse<CardInfoResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CardInfoResponseBean>> call, Response<ApiResponse<CardInfoResponseBean>> response) {
                CardInfoResponseBean bean = response.body().getData();
                fillData(bean);
            }

            @Override
            public void onError(Call<ApiResponse<CardInfoResponseBean>> call, Throwable t) {
                super.onError(call, t);
                finish();
            }
        });

    }

    //获得入场券详情
    private void getShareCardInfo(String id) {
        mAppHelper.apiService().getShareCardInfo(id).enqueue(new BaseCallback<ApiResponse<StadiumCardResponsebean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<StadiumCardResponsebean>> call, Response<ApiResponse<StadiumCardResponsebean>> response) {
                CardInfoResponseBean bean = CardInfoResponseBean.dataChange(response.body().getData());
                fillData(bean);
            }

            @Override
            public void onFailure(Call<ApiResponse<StadiumCardResponsebean>> call, Throwable t) {
                super.onFailure(call, t);
                finish();
            }

            @Override
            public void onError(Call<ApiResponse<StadiumCardResponsebean>> call, Throwable t) {
                super.onError(call, t);
                finish();
            }
        });

    }

    //获得约战入场券详情
    private void getMatchCardInfo(String id) {
        mAppHelper.apiService().getMatchCardInfo(id).enqueue(new BaseCallback<ApiResponse<StadiumCardResponsebean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<StadiumCardResponsebean>> call, Response<ApiResponse<StadiumCardResponsebean>> response) {
                CardInfoResponseBean bean = CardInfoResponseBean.dataChange(response.body().getData());
                bean.setShow_name("约战入场券");
                fillData(bean);
            }

            @Override
            public void onFailure(Call<ApiResponse<StadiumCardResponsebean>> call, Throwable t) {
                super.onFailure(call, t);
                finish();
            }

            @Override
            public void onError(Call<ApiResponse<StadiumCardResponsebean>> call, Throwable t) {
                super.onError(call, t);
                finish();
            }
        });

    }

    //填充数据
    private void fillData(CardInfoResponseBean bean) {
        ticketTypeTextView.setText(bean.getShow_name());
        if (cardType.equals(Type.STADIUM_TYPE) || cardType.equals(Type.STADIUM_TICKET_TYPE)) {
            timeLabelTextView.setText(getResources().getString(R.string.time));
        } else {
            timeLabelTextView.setText("截止时间");
        }
//1.私教卡时间部分都显示无期限;私教卡和多次卡都有次数 ;2.其余无限次 (卡类型 2:多次卡，6：月卡，7：季卡，8：年卡)
        if (bean.getEtime() == 0 || cardType.equals(Type.COACH_TYPE)) {
            timeTextView.setText("无期限");
            countTextView.setText(bean.getNum() + "次");
        } else if (cardType.equals(Type.STADIUM_TYPE) || cardType.equals(Type.STADIUM_TICKET_TYPE) || cardType.equals(Type.STADIUM_YUE) || cardType.equals(Type.STADIUM_FELLOW)) {
            String sTime = StringUtils.getPhpDateFormat(bean.getStime(), new SimpleDateFormat("HH:mm"));
            String eTime = StringUtils.getPhpDateFormat(bean.getEtime(), new SimpleDateFormat("HH:mm"));
            String date = StringUtils.getPhpDateFormat(bean.getEtime(), new SimpleDateFormat("yyyy/MM/dd"));
            timeTextView.setText(StringUtils.getWeekOfDate(bean.getEtime()) + "(" + date + ")" + "\n" + sTime + "-" + eTime);
        } else {
            String date = StringUtils.getPhpDateFormat(bean.getEtime(), new SimpleDateFormat("yyyy/MM/dd"));
            timeTextView.setText(StringUtils.getWeekOfDate(bean.getEtime()) + "(" + date + ")");

            if (bean.getCat_id().equals("2")) {
                countTextView.setText(bean.getNum() + "次");
            } else {
                countTextView.setText("无限次");
            }
        }
        StringBuffer addressCode = new StringBuffer();
        for (int i = 0; i < bean.getAddress().length; i++) {
            addressCode.append(bean.getAddress()[i] + "\n");
        }
        addressTextView.setText(addressCode.substring(0, addressCode.toString().lastIndexOf("\n")).trim());

        StringBuffer placeCode = new StringBuffer();
        for (int i = 0; i < bean.getPlace().length; i++) {
            placeCode.append(bean.getPlace()[i] + "\n");
        }
        if (!placeCode.toString().trim().equals("null")) {
            spaceTextView.setText(placeCode.substring(0, placeCode.toString().lastIndexOf("\n")));
            spaceView.setVisibility(View.VISIBLE);
        } else {
            spaceView.setVisibility(View.GONE);
        }
        StringBuffer iphoneCode = new StringBuffer();
        for (int i = 0; i < bean.getPhone().length; i++) {
            iphoneCode.append(bean.getPhone()[i] + "\n");
        }
        iphoneTextView.setText(iphoneCode);
        qrImageView.setImageBitmap(EncodingUtils.createQRCode(bean.getEwm(), DensityUtils.dip2px($this, 90),
                DensityUtils.dip2px($this, 90),
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));
    }
}
